from odoo import api, models, fields
from odoo.exceptions import ValidationError


class SbDatecoeCache(models.Model):
    _name = 's.datecode.cache'

    name = fields.Char(string='Datecode')
    s_erp_lock_id = fields.Many2one('sb.erp.lock', string='锁定申请')
    s_disable_delivery = fields.Boolean(string='禁止出货', tracking=True)

    def _cron_datecode(self):
        lot_ids = self.env['stock.lot'].sudo().search([('s_week_of_year', '!=', False)])
        lot_datecodes = set(lot_ids.mapped('s_week_of_year'))
        cache_datecodes = set(self.search([]).mapped('name'))
        add_datecode = list(lot_datecodes - cache_datecodes)
        self.create([{'name': d} for d in add_datecode])